package com.cn.shine.controller;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.json.JSONObject;

import com.cn.shine.common.BaseBean;
import com.cn.shine.common.Constant;
import com.cn.shine.common.DataBean;
import com.cn.shine.pojo.Order;
import com.cn.shine.service.OrderService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

@Controller
@RequestMapping("/order")
public class OrderController {

	@Resource
	private OrderService orderService;
	
	@RequestMapping("/orderList")
	public String orderList(){
		return "orderList";
	}
	
	/**
	 * 添加账单信息
	 * @param order
	 * @return
	 */
	@RequestMapping("/addOrder")
	@ResponseBody
	public JSONObject addOrder(Order order){
		BaseBean bean = new BaseBean();
		if(order.getCustomerId()==0){
			bean.setMsg("查询不到该会员信息哦");
			bean.setResult("0");
			return JSONObject.fromObject(bean);
		}
		if(order.getTotalMoney()==null){
		    bean.setMsg("还没输入消费金额哟");
            bean.setResult("0");
            return JSONObject.fromObject(bean);
		}
		int i = orderService.addOrder(order);
		if(i==0){
			bean.setMsg("抱歉，没有生成账单，再试一下呢");
			bean.setResult("0");
		}else{
			bean.setMsg("添加账单成功");
			bean.setResult("1");
		}
		return JSONObject.fromObject(bean);
	}
	
	/**
	 * 删除账单信息
	 * @param id
	 * @return
	 */
	@RequestMapping("/deleteOrder")
	public JSONObject deleteOrder(Integer id){
		BaseBean bean = new BaseBean();
		if(id==null||id==0){
			bean.setMsg("查询不到该账单信息哦");
			bean.setResult("0");
			return JSONObject.fromObject(bean);
		}
		int i = orderService.deleteByPrimaryKey(id);
		if(i==0){
			bean.setMsg("删除失败咯");
			bean.setResult("0");
		}else{
			bean.setMsg("删除成功");
			bean.setResult("1");
		}
		return JSONObject.fromObject(bean);
	}
	
	@RequestMapping("/getOrderListByPage")
	@ResponseBody
	public JSONObject getOrderListByPage(Order order){
	    if(StringUtils.isNotEmpty(order.getFromDate())){
	        order.setFromDate(order.getFromDate()+" 00:00:00");
	        System.out.println(order.getFromDate());
	    }
	    if(StringUtils.isNotEmpty(order.getToDate())){
	        order.setToDate(order.getToDate()+" 23:59:59");
	    }
		List<Order> list = orderService.getOrderListByPage(order);
		int total = orderService.getOrderCountByPage(order);
		JSONObject jsonObject=new JSONObject();
        Gson gson = new GsonBuilder().create();
        jsonObject.put("total", total);
        jsonObject.put("rows", gson.toJson(list));
        return jsonObject;
	}
	
	@RequestMapping("/payTypeRank")
	@ResponseBody
	public JSONObject payTypeRank(){
	    List<Order> list=orderService.getPayTypeRank();
	    String payType="";
	    if(list.size()>0){
	        Order o = list.get(0);
	        payType = o.getPayType();
	    }
	    DataBean bean = new DataBean();
        Gson gson = new GsonBuilder().create();
        bean.setData(gson.toJson(list));
        bean.setMsg(Constant.QUERY_SUCCESS);
        bean.setResult(Constant.SUCCESS);
        JSONObject obj = JSONObject.fromObject(bean);
        obj.put("payType", payType);
        return obj;
	}
	
	@RequestMapping("/custRank")
	@ResponseBody
	public JSONObject getCustRank(){
	    List<Order> list=orderService.getCustRank();
	    String cName="";
	    if(list.size()>0){
	        cName=list.get(0).getcName();
	    }
        DataBean bean = new DataBean();
        Gson gson = new GsonBuilder().create();
        bean.setData(gson.toJson(list));
        bean.setMsg(Constant.QUERY_SUCCESS);
        bean.setResult(Constant.SUCCESS);
        JSONObject obj = JSONObject.fromObject(bean);
        obj.put("cName", cName);
        return obj;
	}
	
	@RequestMapping("/getMonthPayByYear")
	@ResponseBody
	public JSONObject getMonthPayByYear(String year){
		if(StringUtils.isEmpty(year)){
			year = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
		}
		List<Order> list=orderService.getMonthPayByYear(year);
		int totalPay =0;
		for(Order o:list){
		    totalPay+=o.getTotalMoney();
		}
		DataBean bean = new DataBean();
        Gson gson = new GsonBuilder().create();
        bean.setData(gson.toJson(list));
        bean.setMsg(Constant.QUERY_SUCCESS);
        bean.setResult(Constant.SUCCESS);
        JSONObject obj = JSONObject.fromObject(bean);
        obj.put("year", year);
        obj.put("totalPay", totalPay);
        return obj;
	}
}
